﻿namespace WebPacker.Tool.Tests.Sources
{
	using System.Text;
	using NUnit.Framework;
	using WebPacker.Configuration;
	using WebPacker.Filters;
	using WebPacker.Packs;
	using WebPacker.Tool.Sources;
	using WebPacker.Tool.Tests.EqualityComparers;

	[TestFixture]
	public class WebConfigSourceTest
	{
		[Test]
		public void OpenSimpleWebConfigTest()
		{
			var source = new WebConfigSource(@"samples\web.config");
			source.Open();

			Assert.IsNotNull(source.Packs);
			Assert.IsNotNull(source.Configuration);

			var expectedConfiguration = new Configuration
			{
				BasePath = "samples",
				VersionGeneratorName = "lastModified",
				Filters = new IFilter[]
				{
					new YuiCssMinifier(),
					new YuiJavascriptMinifier()
				},
				VersionGenerators = Configuration.GetDefaults().VersionGenerators
			};

			ConfigurationTest.AssertConfiguraionEquals(expectedConfiguration, source.Configuration);

			var expectedPacks = new PackInfo[]
			{
				new PackInfo
				{
					Name = "styles",
					PackType = PackType.Css,
					BasePath = "~/content",
					VersionGeneratorName = "hashCode",
					Resources = new ResourceInfo[]
					{
						new ResourceInfo { Name = "reset.css" },
						new ResourceInfo { Name = "site.css" }
					}
				},
				new PackInfo
				{
					Name = "jquery",
					PackType = PackType.JavaScript,
					BasePath = "~/scripts",
					FilterName = "js",
					Version = "1.5.2",
					Resources = new ResourceInfo[]
					{
						new ResourceInfo { Name = "jquery-1.5.1.js" },
						new ResourceInfo { Name = "jquery.validate.js" }
					}
				}
			};

			Assert.That(expectedPacks, Is.EquivalentTo(source.Packs).Using(new PackInfoEqualityComparer()));
		}

		[Test]
		public void OpenWebConfigTest()
		{
			var source = new WebConfigSource(@"samples\web2.config");
			source.Open();

			Assert.IsNotNull(source.Packs);
			Assert.IsNotNull(source.Configuration);

			var expectedConfiguration = new Configuration
			{
				BasePath = "samples", 
				Output = "~/resources", // from fileSystemProvider.outputPath
				VersionGeneratorName = "md5",
				Filters = new IFilter[]
				{
					new YuiCssMinifier(),
					new YuiJavascriptMinifier()
				},
				VersionGenerators = Configuration.GetDefaults().VersionGenerators
			};

			ConfigurationTest.AssertConfiguraionEquals(expectedConfiguration, source.Configuration);

			var expectedPacks = new PackInfo[]
			{
				new PackInfo
				{
					Name = "scripts",
					PackType = PackType.JavaScript,
					BasePath = @"~\samples", // overrided path
					FilterName = "js",
					Resources = new ResourceInfo[]
					{
						new ResourceInfo { Name = "test1.js", Encoding = Encoding.GetEncoding(1251) },
						new ResourceInfo { Name = "test2.js" }
					}
				},
				new PackInfo
				{
					Name = "styles",
					PackType = PackType.Css,
					FilterName = "css",
					VersionGeneratorName = "manual",
					Resources = new ResourceInfo[]
					{
						new ResourceInfo { Name = @"samples\style1.css" }
					}
				},
			};

			Assert.That(expectedPacks, Is.EquivalentTo(source.Packs).Using(new PackInfoEqualityComparer()));
		}
	}
}
